<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * Copyright (C) 2007  Robert Moss
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 

class RankingObject{
  
  var $configRankingOrder;
  
  var $team;
  var $cnt_matches;
	var $cnt_won;
	var $cnt_draw;
	var $cnt_lost;
	var $sum_points;
	var $neg_points;
	var $sum_matchpart1_result;
	var $sum_matchpart2_result;
	var $sum_matchpart1_legs;
	var $sum_matchpart2_legs;
	var $diff_matchpart_results;
	var $diff_matchpart_legs;
  var $round;
  var $rank;
  var $diff_rank; //not used...
  var $headtohead;
  var $headtohead_diff;
  
  var $stats;
  
  function RankingObject(& $team){
    
    $m1 = array("e1" =>"", "e2"=>"", "l1" =>"", "l2"=>"", "team1"=>"", "team2"=>"","round"=>"");
    $m2 = array("e1" =>"", "e2"=>"", "l1" =>"", "l2"=>"", "team1"=>"", "team2"=>"","round"=>"");
    $m3 = array("e1" =>"", "e2"=>"", "l1" =>"", "l2"=>"", "team1"=>"", "team2"=>"","round"=>"");
    $m4 = array("e1" =>"", "e2"=>"", "l1" =>"", "l2"=>"", "team1"=>"", "team2"=>"","round"=>"");
    
    $this->configRankingOrder = null;
    
    $this->team = $team;
    $this->cnt_matches = 0;
    $this->cnt_won = 0;
    $this->cnt_draw = 0;
    $this->cnt_lost = 0;
    $this->sum_points = 0;
    $this->neg_points = 0;
    $this->sum_matchpart1_result = 0;
    $this->sum_matchpart2_result = 0;
    $this->sum_matchpart1_legs = 0;
    $this->sum_matchpart2_legs = 0;
    $this->diff_matchpart_results = 0;
    $this->diff_matchpart_legs = 0;
    $this->rank = 0;
    $this->headtohead = array();
    $this->headtohead_diff = array();
    $this->stats = array( "cnt_won_home"=> 0,
                          "cnt_won_away"=> 0,
                          "cnt_draw_home"=> 0,
                          "cnt_draw_away"=> 0,
                          "cnt_lost_home"=> 0,
                          "cnt_lost_away"=> 0,
                          "won_highest_home" => $m1,
                          "won_highest_away" => $m2,
                          "lost_highest_home" => $m3,
                          "lost_highest_away" => $m4,
                          "current_ranking"=>0);
  }
  
  function setRankingOrder(& $configRankingOrder){
    $this->configRankingOrder = $configRankingOrder;
  }
  
  function winPct()
  {
    if ($this->cnt_won+$this->cnt_lost+$this->cnt_draw == 0) return 0.5;
    
    return ($this->cnt_won/($this->cnt_won+$this->cnt_lost+$this->cnt_draw));
  }
  
  // This is ONE decison to compare to teams in a table
  // it's call by ranking module to sort the table, and uses tie breaker defined
  // in show.table.xml
  // Should be declared static, but it's not ok for php4 :-(
  function compare($a, $b) 
  {
    $res=0;
    $j=1;
    
    // apply each tie breaker in order
    while ( array_key_exists( $j,$a->configRankingOrder ) and $res ==0 ) {
      switch ($a->configRankingOrder[$j++]) {
        
        case 'points':
          $res=-($a->sum_points - $b->sum_points);
          if ($res==0) {
            $res=($a->neg_points - $b->neg_points);
          } 
          break;
        
        case 'goal_diff':
            $res=-($a->diff_matchpart_results - $b->diff_matchpart_results);
          break;
          
        case 'goal_for':
          $res=-($a->sum_matchpart1_result - $b->sum_matchpart1_result);
          break;
          
        case 'goal_against':
          $res=($a->sum_matchpart2_result - $b->sum_matchpart2_result);
          break;
          
        case 'win_pct':
          $res= -($a->winPct() - $b->winPct());
          if ($res != 0) $res=($res >= 0 ? 1 : -1); 
          break;
          
        case 'gameback':
          $res=-( ($a->cnt_won - $b->cnt_won) + ($b->cnt_lost - $a->cnt_lost) );
          break;
          
        case 'headtohead':
          if ( array_key_exists($b->team->name,$a->headtohead) ) 
            $res = -( $a->headtohead[$b->team->name] );
          break;
          
        case 'headtohead_diff':
          if ( array_key_exists($b->team->name,$a->headtohead_diff) ) 
            $res = -( $a->headtohead_diff[$b->team->name] );
          break;
          
        case 'legs_diff':
          if ( array_key_exists($b->team->name,$a->legs_diff) ) 
            $res = -($a->diff_matchpart_legs - $b->diff_matchpart_legs);
          break;
        
        case 'win_legs':
          if ( array_key_exists($b->team->name,$a->win_legs) ) 
          	$res = -($a->sum_matchpart1_legs - $b->sum_matchpart1_legs);
        	break;
        	
        default;
          break;
      }
    }
    
    return $res;
  }
  
  /* compareWithAlpha calls compare, but adds a team alphabetical sorting 
  if compare return 0 */
  // Should be declared static, but it's not ok for php4 :-(
  function compareWithAlpha($a, $b)
  {
    $res = $a->compare($a, $b);
    if (!$res) $res = strcasecmp($a->team->name, $b->team->name);
    
    return $res;
  }
}

?>
